package my.maven.project;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.UnsupportedEncodingException;

public class PathDemo {

	
	
	public static void main(String[] args) throws Exception {
		String dir = System.getProperty("user.dir");
		System.out.println(dir);
		String filepath = dir + "/src/main/resources/hello.html";
		String filepath1 = PathDemo.class.getClassLoader().getResource("hello.html").getPath();
		
		
		System.out.println(filepath);
//		File file = new File( filepath );
//		FileReader st = new FileReader(file);
//		BufferedReader br = new BufferedReader(st);
//		
//		StringBuilder txt = new StringBuilder();
//		String str;
//		while((str = br.readLine()) != null)
//		{
//			txt.append(str+"\n\r");
//		}
		
//		System.out.println(txt);
		
		System.out.println(filepath1);
//		System.out.println(readToString(filepath1));
		
		String path3 = "/hello.html";
		InputStream inputStream = PathDemo.class.getResourceAsStream(path3);
//		System.out.println(path3);
		System.out.println(readToString(path3));
		
		byte[] buffer = new byte[1024];
		int n = inputStream.read(buffer);
		System.out.println("form file:" + path3 + " read:" + new String(buffer,0,n));
		inputStream.close();
 
		
	}
	
	public static String readToString(String fileName) {  
        String encoding = "UTF-8";  
        File file = new File(fileName);  
        Long filelength = file.length();  
        byte[] filecontent = new byte[filelength.intValue()];  
        try {  
            FileInputStream in = new FileInputStream(file);  
            in.read(filecontent);  
            in.close();  
        } catch (FileNotFoundException e) {  
            e.printStackTrace();  
        } catch (IOException e) {  
            e.printStackTrace();  
        }  
        try {  
            return new String(filecontent, encoding);  
        } catch (UnsupportedEncodingException e) {  
            System.err.println("The OS does not support " + encoding);  
            e.printStackTrace();  
            return null;  
        }  
    }  
	
}
